#include "widget.h"
#include "ui_widget.h"
#include <QNetworkDatagram>
const QString& SERVER_IP = "127.0.0.1";
const qint16& PORT = 8081;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket = new QUdpSocket(this);
    this->setWindowTitle("客户端");
    connect(socket,&QUdpSocket::readyRead,this,&Widget::processResponse);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    const QString &text = ui->lineEdit->text();
    QNetworkDatagram requestDatagram(text.toUtf8(),QHostAddress(SERVER_IP),PORT);

    socket->writeDatagram(requestDatagram);
    ui->listWidget->addItem("客户端："+text);
    ui->lineEdit->clear();

}

void Widget::processResponse()
{
    const QNetworkDatagram& responseDatagram = socket->receiveDatagram();
    QString response = responseDatagram.data();
    ui->listWidget->addItem("服务器："+response);
}
